package com.aceitunaproject.client.commands.implementation;

import com.aceitunaproject.client.commands.Parameter;
import com.aceitunaproject.client.commands.implementation.parameters.TileParameter;
import com.aceitunaproject.client.commands.implementation.parameters.UnitParameter;
import com.aceitunaproject.clientinterface.AceitunaClientInterface;
import com.aceitunaproject.shared.gamelement.Unit;
import com.aceitunaproject.shared.map.MapTile;

/**
 * The order to move an unit to a destination tile
 * 
 * @author Federico Bergstein
 * 
 */
public class MoveUnit extends BasicOrder {
	// TODO: this is a pretty crappy implementation of Order, parameters should
	// be stored in an array or list and moved to BasicOrder, good enough for
	// now but must be improved
	/**
	 * The client interface to use to send the order to the server
	 */
	AceitunaClientInterface clientInterface = null;

	public MoveUnit(AceitunaClientInterface clientInterface) {
		this.clientInterface = clientInterface;
	}

	/**
	 * current parameter
	 */
	private int pos = 0;

	/**
	 * parameter representing the unit to move
	 */
	private Parameter<Unit> movingUnit = new UnitParameter("Unit To Move",
			" Move ", " To");

	/**
	 * parameter representing the tile where the unit is to be moved.
	 */
	private Parameter<MapTile> destinationTile = new TileParameter(
			"Destination Tile", " Tile ", " ");

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aceitunaproject.client.commands.Order#execute()
	 */

	public void execute() {

		clientInterface.moveUnit(movingUnit.getValue().getId(), destinationTile
				.getValue().getPosx(), destinationTile.getValue().getPosy());
		clientInterface.requestMapRefresh();
		executed = true;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aceitunaproject.client.commands.Order#getName()
	 */

	public String getName() {
		return "Move Unit";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aceitunaproject.client.commands.Order#getParameter()
	 */

	public Parameter<?> getParameter() {
		if (pos == 0) {
			return movingUnit;
		} else {
			return destinationTile;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aceitunaproject.client.commands.Order#moveToNextParameter()
	 */

	public boolean moveToNextParameter() {
		if (pos == 0) {
			pos = 1;
			return true;
		} else {
			return false;
		}

	}

	public String getDescription() {

		String desc = "";
		if (movingUnit == null) {
			desc += movingUnit.getPreviousWords() + movingUnit.getValueName()
					+ movingUnit.getLaterWords();
		}
		if (destinationTile == null) {
			desc += destinationTile.getPreviousWords()
					+ destinationTile.getValueName()
					+ movingUnit.getLaterWords();
		}
		return desc;
	}

}
